package system

import (
	"encoding/json"
	"ltbz/cultural_tourism_api/controllers"
	"ltbz/cultural_tourism_api/models"
	"ltbz/cultural_tourism_api/models/system"
	"ltbz/cultural_tourism_api/utils"
	"time"
)

// 小程序配置模块
type SysXcxBannerController struct {
	controllers.BaseAuthController
}

// @Title 新增banner
// @Description 新增banner
// @Param	request	body system.SysXcxBannerReq true "type json string"
// @Success 200
// @router /banner/add [post]
func (this *SysXcxBannerController) BannerAdd() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	var req system.SysXcxBannerReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.BannerUrl == "" {
		br.Msg = "banner路径不能为空"
		return
	}
	xcxBannerObj := new(system.SysXcxBanner)
	xcxBannerObj.BannerUrl = req.BannerUrl
	xcxBannerObj.JumpUrl = req.JumpUrl
	xcxBannerObj.Position = req.Position
	xcxBannerObj.CreateTime = time.Now()
	xcxBannerObj.ModifyTime = time.Now()
	_, err = xcxBannerObj.Add()
	if err != nil {
		br.ErrMsg = "保存失败，Err:" + err.Error()
		br.Msg = "保存失败"
		return
	}
	br.Msg = "保存成功"
	br.Ret = 200
	return
}

// @Title 删除banner
// @Description 删除banner
// @Param	request	body system.SysXcxBannerDelReq true "type json string"
// @Success 200
// @router /banner/delete [post]
func (this *SysXcxBannerController) BannerDelete() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	var req system.SysXcxBannerDelReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.SysXcxBannerId <= 0 {
		br.Msg = "参数错误"
		return
	}
	xcxBannerObj := new(system.SysXcxBanner)
	xcxBannerObj.SysXcxBannerId = req.SysXcxBannerId
	err = xcxBannerObj.Delete()
	if err != nil {
		br.ErrMsg = "删除失败，Err:" + err.Error()
		br.Msg = "删除失败"
		return
	}
	br.Msg = "删除成功"
	br.Ret = 200
	return
}

// @Tags  小程序配置模块
// @Title 获取banner列表
// @Description 获取banner列表
// @Param	request	body system.SysAdminListReq true "type json string"
// @Success 200 {object} system.SysXcxBannerView
// @router /banner/list [post]
func (this *SysXcxBannerController) BannerList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	xcxBannerObj := new(system.SysXcxBanner)
	list, err := xcxBannerObj.GetSysXcxBannerList()
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}

	br.Msg = "获取成功"
	br.Ret = 200
	br.Data = list
	return
}
